package com.share.demo.controller;

import com.share.demo.domain.User;
import com.share.demo.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * caifan 2017/10/3
 */
@RestController
@RequestMapping("/rest/user")
@Slf4j
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping(value = "/saveUser", method = RequestMethod.POST, produces = "application/json;charset=utf-8")
    public void saveUser(@RequestBody User user) {
        user.setCreateTime(new Date());
        userService.insertUser(user);
    }

    @RequestMapping(value = "/saveUser2", method = RequestMethod.POST, produces = "application/json;charset=utf-8")
    public void saveUser2(@RequestBody User user) {
        user.setCreateTime(new Date());
        userService.insertUser(user);
    }

    @RequestMapping(value = "/getUser", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    public ResponseEntity queryUserById(@RequestParam Long id) {
//        log.info("controller getUser id:{}", id);
        return new ResponseEntity(userService.findUserById(id), HttpStatus.OK);
    }

    @RequestMapping(value = "/allUser", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    public ResponseEntity queryAllUser() {
        return new ResponseEntity(userService.findAllUser(), HttpStatus.OK);
    }

    @RequestMapping(value = "/pageUser", method = RequestMethod.GET, produces = "application/json;charset=UTf-8")
    public ResponseEntity queryUserPage() {
        return new ResponseEntity(userService.findUserPage(), HttpStatus.OK);
    }

    /*@RequestMapping(value = "/updateUser", method = RequestMethod.PUT, produces = "application/json;charset=UTf-8")
    public void updateUser(@RequestBody User user) {
        userService.updateUser(user);
    }*/

    @RequestMapping(value = "/updateAge", method = RequestMethod.PUT)
    public void updateAgeById(@RequestParam Long id, @RequestParam Integer age) {
        userService.updateUserAgeById(id, age);
    }

    @RequestMapping(value = "/deleteUser", method = RequestMethod.DELETE)
    public void deleteUserById(@RequestParam Long id) {
        userService.deleteUserById(id);
    }
}
